<?php
/**
 * Flea Site
 *
 * @copyright  Copyright (c) 2008 Movoin Studio (http://movoin.com)
 * @license    Apache License 2.0
 * @version    $Id: News.php 131 2009-05-22 09:53:17Z movoin $
 */

/**
 * Controller_News “公司新闻”控制器
 *
 * @package Site
 * @subpackage Front
 *
 */

//{{include
FLEA::loadClass('Controller_Base');
//}}

class Controller_News extends Controller_Base
{
    var $modelNews;

    /**
     * 构造函数
     *
     * @return Controller_News
     */
    function Controller_News()
    {
        // 构造父类
        parent::Controller_Base();
        $this->modelNews =& FLEA::getSingleton( 'Model_News' );
    }

    function actionIndex()
    {
        //缓存名称
        $cache_id = 'news.index.news';
        //从缓存中读取数据
        $rows = FLEA::getCache($cache_id, $this->lifetime, true, true);
        if(!is_array($rows)) {
            $rows = $this->modelNews->getCategories('news', null, 'cate_id, name');
            if($rows) {
                foreach($rows as $key=>$row) {
                    $rows[$key]['contents'] = $this->modelNews->getContents($row['cate_id'], 'id,title,created');
                }
            }
            FLEA::writeCache( $cache_id, $rows, true );
        }
        $this->_executeView('news', $rows);
    }

    function actionList()
    {
        $classid = (int)$_GET['classid'];
        $pagesize = 15;
        $page = isset($_GET['page']) ? (int)$_GET['page'] : 0;
        //缓存名称
        $cache_id = 'news.list.news.class' . $classid . '.page' . $page;
        //从缓存中读取数据
        $data = FLEA::getCache($cache_id, $this->lifetime, true, true);
        if(!is_array($data)) {
            $table = & $this->modelNews->getTable();
            $where = array(
                array( 'col_key', 'news' ),
                array( 'site_id', 0 ),
                array('cate_id', $classid)
                );
            FLEA::loadHelper( 'pager' );
            $pager =& new FLEA_Helper_Pager($table, $page, $pagesize, $where, 'sort_id ASC, id DESC');
            $data['pager'] = $pager->getPagerData();
            $data['rows'] = $pager->findAll('id,title,created');
            $cate = $this->modelNews->getCategory($classid, 'name');
            $data['cate'] = h($cate['name']);
            FLEA::writeCache( $cache_id, $data, true );
        }
        $this->_executeView('news-list', $data);
    }

    function actionView()
    {
        $newsid = $_GET['newsid']?(int)$_GET['newsid']:null;
        if(!$newsid)
            js_alert('未找到指定新闻', 'history.back(-1)');
        //缓存名称
        $cache_id = 'news.view.news.id' . $newsid;
        //从缓存中读取数据
        $data = FLEA::getCache($cache_id, $this->lifetime, true, true);
        if(!is_array($data)) {
            $data = $this->modelNews->getContent($newsid, 'id, title, created', 'category');
            $data['cate'] = h($data['category']['name']);
            FLEA::writeCache( $cache_id, $data, true );
        }
        $this->modelNews->clickNews($newsid);
        $this->_executeView('news-view', $data);
    }

}
